package com.zhongxi.guoyuan.adapter.personExchangeOrder;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.clearweed.PersonExchangeResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.DataUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by qj on 2018/4/13.
 */

public class PersonExchangeNoSendAdapter extends BaseAdapter {

    private String jsonOrderCus;
    private String jsonShopInfo;
    private Handler mHandler;
    private Context mContext;
    private List<PersonExchangeResult.OrderInfo> mInfoList;
    private PersonExchangeResult.OrderInfo mOrderInfo;
    private JSONObject mshopInfo;

    private String latitude, longitude;
    private JSONObject mShop;

    public PersonExchangeNoSendAdapter(Handler handler, Context context) {
        mHandler = handler;
        mContext = context;
    }

    public void setData(List<PersonExchangeResult.OrderInfo> infoList, String jsonOrderCus, String jsonShopInfo) {
        mInfoList = infoList;
        this.jsonOrderCus = jsonOrderCus;
        this.jsonShopInfo = jsonShopInfo;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mInfoList != null && mInfoList.size() > 0 ? mInfoList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mViewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.person_exchange_item,null);
            mViewHolder = new ViewHolder(convertView);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }

        if (null != mInfoList && !"".equals(mInfoList)) {
            mOrderInfo = mInfoList.get(position);
            if (jsonShopInfo != null) {
                try {
                    mshopInfo = new JSONObject(jsonShopInfo);
                    mShop = mshopInfo.optJSONObject(mOrderInfo.getShopid());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            if (mOrderInfo != null) {
                if ((null != mOrderInfo.getOrdertype() && mOrderInfo.getOrdertype().equals("1"))) {//email
                    mViewHolder.mPersonExchangeBgColor.setBackgroundColor(mContext.getResources().getColor(R.color.cart_background));
                    mViewHolder.mPresonExchangeType.setTextColor(mContext.getResources().getColor(R.color.title_red));
                    mViewHolder.mPersonExchangeNum.setTextColor(mContext.getResources().getColor(R.color.title_red));

                    mViewHolder.mPersonExchangeAddressInfo.setVisibility(View.GONE);
                    mViewHolder.mPersonExchagneShop.setVisibility(View.VISIBLE);

                    //去支付----》待发货
                    mViewHolder.mPersonExchangeToPay.setBackground(null);
                    mViewHolder.mPersonExchangeToPay.setText("待发货");
                    mViewHolder.mPersonExchangeToPay.setTextColor(mContext.getResources().getColor(R.color.postCartItem));
                    mViewHolder.mPersonExchangeDelete.setVisibility(View.GONE);

                    // 查物流----》联系商家 hide 确认收货
                    mViewHolder.mPersonExchangeSendstate.setVisibility(View.VISIBLE);
                    mViewHolder.mPersonExchangeConfirm.setVisibility(View.GONE);
                    mViewHolder.mPersonExchangeToView.setText("联系商家");



                    if (null != mOrderInfo.getId() && !"".equals(mOrderInfo.getId())) {
                        mViewHolder.mPresonExchangeType.setText("邮寄：" + mOrderInfo.getId());
                    } else {
                        mViewHolder.mPresonExchangeType.setText("邮寄");
                    }

                    if (null != mShop && !"".equals(mShop)) {
                        mViewHolder.mPersonExchagneShop.setText("商家：" + mShop.optString("name"));

                        mViewHolder.mPersonExchangeToView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (mShop.optString("status").equals("1")){
                                    if (null != mShop.optString("phone") && !"".equals(mShop.optString("phone"))) {
                                        Intent intent = new Intent(Intent.ACTION_DIAL);
                                        Uri data = Uri.parse("tel:" + mShop.optString("phone"));
                                        intent.setData(data);
                                        mContext.startActivity(intent);
                                    } else {
                                        Toast.makeText(mContext, "联系方式不可用", Toast.LENGTH_SHORT).show();
                                    }
                                }else {
                                    Toast.makeText(mContext, "店铺已关闭", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }
            }

            Glide.with(mContext)
                    .load(Url.IMG_URL + mOrderInfo.getThumbnail())
                    .placeholder(R.mipmap.hicc)
                    .fitCenter()
                    .into(mViewHolder.mPersonExchagneImage);

            if (null != mOrderInfo.getGoodsname() && !"".equals(mOrderInfo.getGoodsname())) {
                mViewHolder.mPersonExchangeName.setText(mOrderInfo.getGoodsname());
            } else {
                mViewHolder.mPersonExchangeName.setText("");
            }
            if (null != mOrderInfo.getCtime() && !"".equals(mOrderInfo.getCtime())) {
                mViewHolder.mPersonExchagnePayTime.setText("下单：" + mOrderInfo.getCtime());
            } else {
                mViewHolder.mPersonExchagnePayTime.setText("下单：");
            }

            if (null != mOrderInfo.getGattr() && !"".equals(mOrderInfo.getGattr())) {
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(mOrderInfo.getGattr());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Iterator<String> keys = jsonObject.keys();
                String attr = "";
                while (keys.hasNext()) {
                    String next = keys.next();
                    String attrValue = jsonObject.optString(next);
                    attr += attrValue + "/";
                }
                mViewHolder.mPersonExchangeAttr.setText(attr);
            } else {
                mViewHolder.mPersonExchangeAttr.setText("");
            }

            if (null != mOrderInfo.getGoodsnum() && !"".equals(mOrderInfo.getGoodsnum())) {
                mViewHolder.mPersonExchangeNum.setText(mOrderInfo.getGoodsnum() + "件");
            } else {
                mViewHolder.mPersonExchangeNum.setText("");
            }

            if (DataUtil.moreThanZero(mOrderInfo.getInnerprice()) && DataUtil.moreThanZero(mOrderInfo.getRedbean())) {
                mViewHolder.mPersonExchangeMeidou.setText(mOrderInfo.getInnerprice() + "美豆");
                mViewHolder.mPersonExchagneHongdou.setText("+" + mOrderInfo.getRedbean() + "红豆");
            } else if (DataUtil.moreThanZero(mOrderInfo.getInnerprice()) && !DataUtil.moreThanZero(mOrderInfo.getRedbean())) {
                mViewHolder.mPersonExchangeMeidou.setText(mOrderInfo.getInnerprice() + "美豆");
                mViewHolder.mPersonExchagneHongdou.setText("");
            } else if (!DataUtil.moreThanZero(mOrderInfo.getInnerprice()) && DataUtil.moreThanZero(mOrderInfo.getRedbean())) {
                mViewHolder.mPersonExchangeMeidou.setText("");
                mViewHolder.mPersonExchagneHongdou.setText(mOrderInfo.getRedbean() + "红豆");
            }else {
                mViewHolder.mPersonExchangeMeidou.setText("");
                mViewHolder.mPersonExchagneHongdou.setText("");
            }
            if ((DataUtil.moreThanZero(mOrderInfo.getRedbean())&&DataUtil.moreThanZero(mOrderInfo.getPrice()))||(DataUtil.moreThanZero(mOrderInfo.getInnerprice())&&DataUtil.moreThanZero(mOrderInfo.getPrice()))){
                mViewHolder.mPersonExchangeMoney.setText("+"+mOrderInfo.getPrice()+"元");
            }else if (DataUtil.moreThanZero(mOrderInfo.getPrice())){
                mViewHolder.mPersonExchangeMoney.setText(mOrderInfo.getPrice()+"元");
            }else {
                mViewHolder.mPersonExchangeMoney.setText("");
            }

        }
        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.person_exchange_name)
        TextView mPersonExchangeName;
        @Bind(R.id.preson_exchange_type)
        TextView mPresonExchangeType;
        @Bind(R.id.person_exchange_num)
        TextView mPersonExchangeNum;
        @Bind(R.id.person_exchagne_image)
        ImageView mPersonExchagneImage;
        @Bind(R.id.person_exchagne_payTime)
        TextView mPersonExchagnePayTime;
        @Bind(R.id.person_exchagne_shop)
        TextView mPersonExchagneShop;
        @Bind(R.id.person_exchange_attr)
        TextView mPersonExchangeAttr;
        @Bind(R.id.person_exchange_meidou)
        TextView mPersonExchangeMeidou;
        @Bind(R.id.person_exchagne_hongdou)
        TextView mPersonExchagneHongdou;
        @Bind(R.id.person_exchagne_money)
        TextView mPersonExchangeMoney;
        @Bind(R.id.person_exchange_toPay)
        TextView mPersonExchangeToPay;
        @Bind(R.id.person_exchange_delete)
        TextView mPersonExchangeDelete;

        @Bind(R.id.person_exchange_confirm)
        TextView mPersonExchangeConfirm;
        @Bind(R.id.person_exchange_status)
        TextView mPersonExchangeStatus;
        @Bind(R.id.person_exchange_toView)
        TextView mPersonExchangeToView;
        @Bind(R.id.person_exchange_sendstate)
        RelativeLayout mPersonExchangeSendstate;
        @Bind(R.id.person_Exchange_shopName)
        TextView mPersonExchangeShopName;

        @Bind(R.id.person_exchange_shopPhone)
        TextView mPersonExchangeShopPhone;

        @Bind(R.id.person_exchange_shopAdderss)
        TextView mPersonExchangeShopAdderss;

        @Bind(R.id.person_exchange_call)
        TextView mPersonExchangeCall;
        @Bind(R.id.person_exchange_navigate)
        TextView mPersonExchangeNavigate;
        @Bind(R.id.person_exchange_addressInfo)
        LinearLayout mPersonExchangeAddressInfo;
        @Bind(R.id.person_exchange_bgColor)
        RelativeLayout mPersonExchangeBgColor;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
